package cn.cars.servlet;

import cn.cars.pojo.Car;
import cn.cars.service.CarService;
import cn.cars.service.impl.CarServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/modify")
public class ModifyServlet extends HttpServlet {
    private CarService service = new CarServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        Double price = Double.valueOf(req.getParameter("price"));
        String mileage = req.getParameter("mileage");
        String buyTime = req.getParameter("buyTime");
        int type = Integer.parseInt(req.getParameter("type"));
        int status = Integer.parseInt(req.getParameter("status"));

        Car car = new Car(id, name, price, mileage, buyTime, type, status);

        int result = service.modify(car);

        PrintWriter out = resp.getWriter();

        if (result > 0){
            out.println("<script>alert('修改成功！');location=href='list';</script>");
        }else {
            out.println("<script>alert('修改失败！');location=href='list';</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
